--
-- Author: yawei.zhu
-- Date: 2018-08-03 10:40:44
--
-- RoadHeroPanel
--

local RoadHeroRender = import(".renders.RoadHeroRender")

local ClassRef = app.mvc.createPanelClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.RoadHeroNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self._ui.tlt:setString( L("task_road_desc17") )
    self._listView = self._ui.ListView

    self:refresh()
end

function ClassRef:refresh()
    self._listView:removeAllChildren()

    local length = GD:getHeroRoadLength()
    for i = 1, length do 
        local render = RoadHeroRender.create()
        render:onBtnClick(self._onOperate)
        render:setData(i)
        self._listView:pushBackCustomItem(render)
    end
    self._listView:refreshView() 
    self._listView:setVisible(false) 

    self._bAlreadyUpdate = nil
end

-- 更新英雄之路进度
function ClassRef:updateRoadHeroInfo(process)
    self._listView:setVisible(true)

    local items = self._listView:getItems()
    for _, item in ipairs(items) do
        item:updateHeroInfo(process)
    end

    -- 第一次自动滚动到当前进度的位置
    if not self._bAlreadyUpdate then
        self._bAlreadyUpdate = true

        local length = #items
        local level = process.level 
        if process.step >= 3 then level = level + 1 end
        if length > 4 and level > 3 then
            local percent = (level - 3) / (length - 4) * 100
            if percent > 100 then percent = 100 end
            self._listView:scrollToPercentVertical(percent, 0.1, false)
        end
    end
end


function ClassRef:onOperate(cb)
    self._onOperate = cb
end

return ClassRef
